<html>
<head><meta charset="utf-8"><title>Rename refactor · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rename.20refactor.html">Rename refactor</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222999868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rename%20refactor/near/222999868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rename.20refactor.html#222999868">(Jan 16 2021 at 19:04)</a>:</h4>
<p>So I have been looking into rewriting the rename module to instead use the <code>FindUsages</code> functionality directly. As said in <a href="https://github.com/rust-analyzer/rust-analyzer/issues/4290">https://github.com/rust-analyzer/rust-analyzer/issues/4290</a> we ideally want to base renaming on the AST instead of strings so we can properly rename a reference by first figuring out how it is used, as in for example if its part of a destructure etc. For that I was wondering how we would do that ideally, given currently search gives us FileReferences:<br>
<a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ide_db/src/search.rs#L56-L72">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ide_db/src/search.rs#L56-L72</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[derive(Debug, Clone)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">FileReference</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">range</span>: <span class="nc">TextRange</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">kind</span>: <span class="nc">ReferenceKind</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">access</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">ReferenceAccess</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[derive(Debug, Clone, PartialEq)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">ReferenceKind</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">FieldShorthandForField</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">FieldShorthandForLocal</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">StructLiteral</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">RecordFieldExprOrPat</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">SelfParam</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">EnumLiteral</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Lifetime</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Other</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>My initial thought was to get rid of the current ReferenceKind entirely and replace it with something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[derive(Debug, Clone)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">ReferenceKind</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">NameRef</span><span class="p">(</span><span class="n">ast</span>::<span class="n">NameRef</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Name</span><span class="p">(</span><span class="n">ast</span>::<span class="n">Name</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Lifetime</span><span class="p">(</span><span class="n">ast</span>::<span class="n">Lifetime</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="223000033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rename%20refactor/near/223000033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rename.20refactor.html#223000033">(Jan 16 2021 at 19:07)</a>:</h4>
<p>makes sense as a long term plan!</p>
<p>but i'd start with just replacing ide/references with ide_db/search, to keep the diff smaller</p>



<a name="223000101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Rename%20refactor/near/223000101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Rename.20refactor.html#223000101">(Jan 16 2021 at 19:08)</a>:</h4>
<p><span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>